package com.example.back.controller;

import com.example.back.model.User;
import com.example.back.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 用户控制层
 */
@RestController
@RequestMapping("/user")
public class UserController {
    private static final Logger logger = LogManager.getLogger(UserController.class);

    @Resource
    private UserService userService;

    @RequestMapping("/login")
    public boolean login(@RequestParam("account") String account, @RequestParam("password") String password){
        logger.info("account: " + account + "; password: " + password);
        User user = userService.findByAccount(account);
        if(user == null) {
            user = userService.findByPhone(account);
            if(user == null)
                return false;
            else return user.getPassword().equals(password);
        }
        else return user.getPassword().equals(password);
    }

    @RequestMapping("/register")
    public String register(@RequestParam("account") String account, @RequestParam("username") String username, @RequestParam("phone") String phone, @RequestParam("password") String password){
        logger.info("account: " + account + ";username" + username + "; phone: " + phone + "; password: " + password);
        User user = userService.findByAccount(account);
        if(user == null){
            user = userService.findByPhone(phone);
            if(user == null){
                try{
                    userService.addUser(account, username, password, phone);
                    return "true";
                }catch (Exception e){
                    e.printStackTrace();
                }return "false";
            }
            else return "phone_false";
        }
        else return "account_false";
    }

    //更新用户信息
    @RequestMapping("/musername")
    public Boolean modifyUsername(@RequestParam("account") String account, @RequestParam("username") String username){
        return userService.updateUsername(account, username);
    }
    @RequestMapping("/msex")
    public Boolean modifySex(@RequestParam("account") String account, @RequestParam("sex") int sex){
        return userService.updateSex(account, sex);
    }
    @RequestMapping("/mphone")
    public Boolean modifyPhone(@RequestParam("account") String account, @RequestParam("phone") String phone){
        return userService.updatePhone(account, phone);
    }
    @RequestMapping("/memail")
    public Boolean modifyEmail(@RequestParam("account") String account, @RequestParam("email") String email){
        return userService.updateEmail(account, email);
    }
    @RequestMapping("/mpassword")
    public Boolean modifyPassword(@RequestParam("account") String account, @RequestParam("password") String password){
        return userService.updatePassword(account, password);
    }
    @RequestMapping("/mphoto")
    public Boolean modify(@RequestParam("account") String account, @RequestParam("photo") String photo){
        return userService.updatePhoto(account, photo);
    }


    @RequestMapping("/getUserInformation/{account}")
    @ResponseBody
    public User getUserInformation(@PathVariable(name = "account") String account){
        logger.info("account: " + account);
        User user = userService.findByAccount(account);
        if(user == null)user = userService.findByPhone(account);
        return user;
    }

    @RequestMapping("/getUserList")
    @ResponseBody
    public List<User> getUserList(){
        return userService.findAllUser();
    }

    @RequestMapping("/updateUser")
    public String updateUser(int id, String password, String username, String sex, String phone, String email) {
        String rs = "";
        if (userService.updateUser(id, password, username, sex, phone, email)) {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>用户信息更新成功！点击返回（3秒后自动返回）</button>";
        } else {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>用户信息更新失败！点击返回（3秒后自动返回）</button>";
        }
        return rs;
    }

    @RequestMapping("/insertUser")
    public String insertUser(String account, String password, String username, String sex, String phone, String email) {
        User user = userService.findByAccount(account);
        if(user == null){
            user = userService.findByPhone(phone);
            if(user == null){
                try{
                    userService.insertUser(account, password, username, sex, phone, email);
                    return "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>用户添加成功！点击返回（3秒后自动返回）</button>";
                }catch (Exception e){
                    e.printStackTrace();
                }return "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>用户添加失败！点击返回（3秒后自动返回）</button>";
            } else {
                return "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>手机号已被占用！点击返回（3秒后自动返回）</button>";
            }
        } else {
            return "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>账号已被占用！点击返回（3秒后自动返回）</button>";
        }
    }

    @RequestMapping("deleteUser")
    public String deleteUser(int id) {
        String rs = "";
        if (userService.deleteUser(id)) {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>用户注销成功！点击返回（3秒后自动返回）</button>";
        } else {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>用户注销失败！点击返回（3秒后自动返回）</button>";
        }
        return rs;
    }

    @RequestMapping("watchPhoto")
    public String watchPhoto(int id) {
        return "<img src='" + userService.findPhotoByID(id) + "'>";
    }
}
